package com.jd.org.controller;

import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Department;
import com.jd.org.query.DepartmentQuery;
import com.jd.org.service.IDepartmentService;
import com.jd.basic.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 加载一个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
     * 加载所有对象
     * @return
     */
    @GetMapping
    public List<Department> findAll(){
        return departmentService.loadAll();
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
       try {
           departmentService.remove(id);
           return new AjaxResult();
       } catch (Exception e) {
           e.printStackTrace();
           return new AjaxResult(false,"删除失败");
       }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }


    /**
     * 增加和修改功能
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){

                departmentService.save(department);
            } else {
                System.out.println(department.toString());
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"操作失败");
        }
    }



    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/department + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(name = "部门列表",sn = "department:list") //value后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
